<?php

trait Trait_Api_Visa
{

    /**
     * 获取热门签证国家
     * User: netman
     * Date: 2023/10/16 13:29
     */
    public function get_hot_visa_country()
    {
        $str = 'id,pid,kindname,litpic,bigpic,countrypic,jieshao,pinyin,english';
        $area = DB::select(DB::expr($str))->from('visa_area')->where("pid<1 and isopen=1")
            ->order_by('displayorder', 'asc')->execute()->as_array();
        if (count($area) > 0) {
            foreach ($area as &$v) {
                $country = DB::select(DB::expr($str))->from('visa_area')
                    ->where("pid={$v['id']} and isopen=1 and ishot=1")
                    ->order_by('displayorder', 'asc')
                    ->execute()->as_array();
                foreach ($country as &$val) {
                    $val['litpic'] = $this->img_url($val['litpic'], 160, 160);
                    $val['bigpic'] = $this->img_url($val['bigpic']);
                    $val['countrypic'] = $this->img_url($val['countrypic']);
                }
                $v['country'] = $country;
            }
        }
        return $area;
    }

    /**
     * 按顺序获取签证
     * @param $page
     * @param $pagesize
     * @return array
     * User: netman
     * Date: 2023/10/16 13:37
     */
    public function get_visa_by_order($page, $pagesize)
    {
        $offset = ($page - 1) * $pagesize;
        $arr = DB::select("visa.*")->from('visa')->join('allorderlist', 'left')
            ->on('visa.id', '=', 'allorderlist.aid')
            ->on('allorderlist.typeid', '=', self::TYPE_ID)
            ->where('visa.ishidden', '=', 0)
            ->offset($offset)
            ->limit($pagesize)
            ->execute()
            ->as_array();
        return $this->format_visa($arr);
    }

    /**
     * 格式化数据
     * @param $arr
     * @return array
     * User: netman
     * Date: 2023/10/12 13:45
     */
    private function format_visa($arr, $mid = 0)
    {
        if (empty($arr)) {
            return $arr;
        }

        $marketing_strategy = Model_Api_V2_Marketing_Strategy::get_instance(self::TYPE_ID, array('mid' => $mid));
        $data = array();
        foreach ($arr as $v) {
            $out                 = [];
            $out['id']           = $v['id'];
            $out['title']        = $v['title'];
            $out['handleday']    = $v['handleday'];
            $out['litpic']       = $this->img_url($v['litpic'], 750, 750);
            $out['satisfyscore'] = Model_Comment::get_score($v['id'], self::TYPE_ID, $v['satisfyscore'], $v['commentnum']);//满意度
            $out['satisfyscore'] = $out['satisfyscore']>0?$out['satisfyscore'].'%':'';
            $out['sellnum']      = Model_Member_Order::get_sell_num($v['id'], self::TYPE_ID) + (int)$v['bookcount'];
            $out['sellnum']      = $out['sellnum'] > 999 ? '999+' : $out['sellnum'];
            $out['icon_list']    = $this->icons($v['iconlist']);
            $city                = Model_Visa::visa_city_by_id($v['cityid']);
            if (is_array($city)) {
                $city = $city['kindname'];
            }
            if($v['attrlist']){
                $out['attr_list'] = Model_Visa::get_attr_list($v['attrlist']);
            }else{
                $out['attr_list'] = [];
            }
            $out['city']               = $city;
            $out['price']              = Currency_Tool::price($v['price']);
            $visatypename              = DB::select('kindname')->from('visa_kind')->where('id', '=', $v['visatype'])->execute()->current();
            $out['visatypename']       = $visatypename['kindname'];
            $out['marketing_strategy'] = $marketing_strategy->get_support_application($out['id']);
            $data[]                    = $out;
            unset($out);
        }
        return $data;
    }

    /**
     * 获取区域下级
     * @param $pid
     * @return array
     * User: netman
     * Date: 2023/10/16 14:18
     */
    private function get_area_child($pid)
    {
        $pid = $pid ? $pid : 0;
        return DB::select('id', 'kindname', 'pid', 'pinyin')->from('visa_area')
            ->where('pid', '=', $pid)
            ->and_where('isopen', '=', 1)
            ->and_where('webid', '=', 0)
            ->order_by('displayorder', 'asc')
            ->execute()
            ->as_array();
    }

    /**
     * @desc 属性生成where条件,用于多条件属性搜索
     * @param $attrid
     * @return string
     */
    private function get_attr_where($attrid)
    {
        $arr = Common::remove_arr_empty(explode('_', $attrid));
        foreach ($arr as $value) {
            $value = (int)$value;
            if ($value != 0) {
                $str .= " and FIND_IN_SET($value,sline_visa.attrid) ";
            }
        }

        return $str;
    }

    /**
     * 获取内容详情
     * @param $params
     * @return array
     * User: netman
     * Date: 2023/10/12 13:59
     */
    private function get_detail_content($params)
    {
        $default = array(
            'typeid' => '8',
            'productinfo' => 0,
            'onlyrealfield' => 1,
            'pc' => 0

        );
        $params = array_merge($default, $params);
        /**
         * @var array $productinfo
         */
        extract($params, true);
        $arr = DB::select()->from('visa_content')
            ->where('webid', '=', 0)
            ->and_where('isopen', '=', 1)
            ->order_by('displayorder', 'ASC')
            ->execute()
            ->as_array();

        //扩展表数据
        $productid = $productinfo['id'];//产品id
        $ar = DB::select()->from('visa_extend_field')->where('productid', '=', $productid)->execute()->as_array();
        $list = array();
        foreach ($arr as $v) {
            if ($v['columnname'] == 'tupian') {
                continue;
            }
            if ($v['isrealfield'] == 1) {
                $content = ! empty($productinfo[$v['columnname']]) ? $productinfo[$v['columnname']] : $ar[0][$v['columnname']];
                $content = $content ? $content : '';
                if (empty($content)) {
                    continue;
                }
            }
            $a = array();
            $a['columnname'] = $v['columnname'];
            $a['chinesename'] = $v['chinesename'];
            $a['content'] = Model_Api_V2_Xcx::filter_content($content);
            $list[] = $a;
        }
        return $list;
    }


}